﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XinYi.CrookedShow.DAL;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShow.BLL
{
    public class BaseService<T> where T : class, new()

    {
        //数据访问层
        private readonly BaseRepository<T, CrookedShowEntities> baseRepository = new BaseRepository<T, CrookedShowEntities>();

        #region 添加
        /// <summary>
        /// 单表的添加一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Add(T entity)
        {
            return baseRepository.Add(entity);
        }

        /// <summary>
        /// 单表的添加多条记录
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public virtual bool AddRange(List<T> entities)
        {
            return baseRepository.AddRange(entities);
        }

        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Update(T entity)
        {
            return baseRepository.Update(entity);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除单个实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(T entity)
        {
            return baseRepository.Delete(entity);
        }

        /// <summary>
        /// 删除单个实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(int id)
        {
            return baseRepository.Delete(id);
        }

        /// <summary>
        /// 删除多个实体
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public virtual bool BatchDelete(List<T> entities)
        {
            return baseRepository.BatchDelete(entities);
        }

        /// <summary>
        /// 删除多个实体根据id
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public virtual bool BatchDelete(params int[] ids)
        {
            return baseRepository.BatchDelete(ids);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询返回一个集合
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public virtual List<T> QueryList(Expression<Func<T, bool>> lambdaExpression)
        {
            return baseRepository.QueryList(lambdaExpression);
        }

        /// <summary>
        /// 查询返回一个对象,没有返回null
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public virtual T Query(Expression<Func<T, bool>> lambdaExpression)
        {
            return baseRepository.Query(lambdaExpression);
        }

        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public virtual bool Exists(Expression<Func<T, bool>> lambdaExpression)
        {
            return baseRepository.Exists(lambdaExpression);
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderbyLambda"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public virtual List<T> QueryPageList<S>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, out int count, bool isAsc = true)
        {
            return baseRepository.QueryPageList(pageIndex, pageSize, whereLambda, orderbyLambda, out count, isAsc);
        }
        #endregion

    }
}

